package com.sun.app.service.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import sun.core.json.JSONArray;
import sun.core.json.JSONObject;

import com.sun.app.service.BaseService;
import com.sun.core.Constants;
import com.sun.core.util.MD5;
import com.sun.dao.CUserHdphotoMapper;
import com.sun.dao.CUserMapper;
import com.sun.dao.CUserSettingMapper;
import com.sun.dao.CUserTipMapper;
import com.sun.util.AppResult;
import com.sun.util.PinyingUtil;
import com.sun.util.TimeUtil;
import com.sun.util.limit.CompareUtil;
import com.sun.util.limit.LimitType;
import com.sun.vo.CUser;
import com.sun.vo.CUserExample;
import com.sun.vo.CUserHdphoto;
import com.sun.vo.CUserHdphotoExample;
import com.sun.vo.CUserSetting;
import com.sun.vo.CUserSettingExample;
import com.sun.vo.CUserTip;
import com.sun.vo.CUserTipExample;

/**
 * @author LH
 * 
 */
public class UserService extends BaseService {

	@Autowired
	private CUserMapper userMapper;
	@Autowired
	private CUserSettingMapper settingMapper;
	@Autowired
	private CUserHdphotoMapper photoMapper;
	@Autowired
	private CUserTipMapper tipMapper;

	public AppResult updateUserSetting(HttpServletRequest request) {
		AppResult rs = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		if (UID != null) {
			String newSettingStr = request.getParameter("setting");
			String[] settingArray = newSettingStr.split(",");
			CUserSettingExample example = new CUserSettingExample();
			example.createCriteria().andUidEqualTo(UID);
			CUserSetting setting = new CUserSetting();
			int i = 0;
			setting.setSetbase(settingArray[i++]);
			setting.setSetstatus(settingArray[i++]);
			setting.setSetschool(settingArray[i++]);
			setting.setSettip(settingArray[i++]);
			setting.setSetmsg(settingArray[i++]);
			setting.setSetpublish(settingArray[i++]);
			setting.setSetround(settingArray[i++]);
			setting.setSetactivity(settingArray[i++]);
			int r = settingMapper.updateByExampleSelective(setting, example);
			if (r > 0) {
				rs.setStatus(200);
				rs.setMsg("success");
				return rs;
			}

		}
		rs.setStatus(500);
		rs.setMsg("error");
		return rs;
	}

	public AppResult updateTip(HttpServletRequest request) {
		AppResult r = new AppResult();
		try {
			String UID = (String) request.getSession().getAttribute("UID");
			CUserTipExample example = new CUserTipExample();
			example.createCriteria().andUidEqualTo(Integer.valueOf(UID));
			int countDelete = tipMapper.deleteByExample(example);
			String tips = request.getParameter("tips");
			String[] tipArray = tips.split(",");
			System.out.println("TIP   "+tips);
			if (tipArray.length > 0) {
				CUserTip tipVo = new CUserTip();
				for (String t : tipArray) {
					tipVo.setUid(Integer.valueOf(UID));
					tipVo.setTip(Integer.valueOf(t));
					tipMapper.insertSelective(tipVo);
				}
				r.setStatus(200);
				r.setResult("success");
			} else {

			}

		} catch (Exception e) {
			r.setStatus(500);
			r.setResult("error");
			return r;
		}
		return r;
	}

	// 更新头像方法
	public AppResult uploadPhoto(HttpServletRequest request) {
		AppResult r = new AppResult();
		try {
			String UID = (String) request.getSession().getAttribute("UID");
			String path = request.getParameter("path");
			CUserHdphotoExample example = new CUserHdphotoExample();
			example.createCriteria().andUidEqualTo(UID);
			CUserHdphoto userPhoto = new CUserHdphoto();
			userPhoto.setLimg(path);
			userPhoto.setLpath(path);
			userPhoto.setMimg(path);
			userPhoto.setMpath(path);
			userPhoto.setSimg(path);
			userPhoto.setSpath(path);

			int count = photoMapper
					.updateByExampleSelective(userPhoto, example);
			if (count > 0) {
				r.setStatus(200);
				r.setResult("success");
			} else {
				r.setStatus(500);
				r.setResult("error");

			}
		} catch (Exception e) {
			r.setStatus(500);
			r.setResult("error");
			return r;
		}
		return r;
	}

	public AppResult updateUserBase(HttpServletRequest request) {
		AppResult rs = new AppResult();
		String UID = (String) request.getSession().getAttribute("UID");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String birthday = request.getParameter("birthday");
		String school = request.getParameter("school");
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日");
		CUser user = new CUser();
		try {
			Date parse = fmt.parse(birthday);
			System.out.println(parse);
			user.setBirth(parse);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String introduce = request.getParameter("introduce");

		CUserExample example = new CUserExample();
		example.createCriteria().andUidEqualTo(UID);
		user.setUsername(name);
		user.setUsertruename(name);
		user.setUsersex(sex);
		user.setCity(city);
		user.setProvince(province);
		user.setIntroduce(introduce);
		user.setSchool(school);

		int count = userMapper.updateByExampleSelective(user, example);
		if (count > 0) {
			rs.setStatus(200);
			rs.setResult("success");
		} else {
			rs.setStatus(300);
			rs.setResult("error");
		}
		return rs;
	}

	// 查询参与好友

	private static final String SELECT_ATTEND_FRIEND_LIST = "select FRIENDID,SIMG,(select USERTRUENAME from c_user where UID=F.FRIENDID) from c_user cu,c_user_hdphoto cp,c_activity_attention caa,c_friend f "
			+ "where caa.UID=cu.UID and cu.UID=cp.UID and f.UID=cu.UID and ACTID=? and cu.UID=? and f.STATE=1";

	public List<Map<String, Object>> getAttendFriendList(String UID, int actId) {
		return jt.queryForList(SELECT_ATTEND_FRIEND_LIST, actId, UID);
	}

	private static final String COUNT_FRIEND = "select count(*) from c_friend where UID=? and state=1";

	public int getCountOfFriend(String UID) {
		return jt.queryForInt(COUNT_FRIEND, UID);
	}

	private static final String QUERY_USER = "select USERID,MIMG,USERTRUENAME,c_user.UID from c_user,c_user_hdphoto where c_user_hdphoto.UID = c_user.UID and c_user.EMAIL = ?";
	private static final String QUERY_USERSCHOOL_BY_ID = "select schoolname from m_school where schoolid = ?";

	public AppResult checkUser(HttpServletRequest request) {
		System.out.println("登陆");
		AppResult r = new AppResult();
		String email = request.getParameter("email");
		String passwd = request.getParameter("passwd");
		String actId = request.getParameter("actId");
		passwd = MD5.getMD5ofStr(passwd);
		System.out.println("LOGIN E" + email + "P" + passwd);
		
		try {
			Map<String, Object> userDB = jt.queryForMap(QUERY_USER, email);
			System.out.println(userDB);
			if (userDB != null
					&& (passwd.equals("iscookie!@#$%^&*()") || passwd
							.equals(passwd))) {
				System.out.println("登陆成功");

				int friendCount = getCountOfFriend((String) userDB.get("UID"));
				// 用户信息存入session
				request.getSession().setAttribute("UID", userDB.get("UID"));

				request.getSession().setAttribute("USERTRUENAME",
						userDB.get("USERTRUENAME"));
				userDB.put("friendCount", friendCount);

				Map userById = getUserById((String) userDB.get("UID"),null);
				List userSchoolNameList = jt
						.queryForList(QUERY_USERSCHOOL_BY_ID, (String) userById
								.get("SCHOOL"));
				System.out.println(userSchoolNameList);
				try {
					userById.put("SCHNAME", ((Map) userSchoolNameList.get(0))
							.get("schoolname"));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				System.out.println(userById);
				r.setStatus(200);
				r.setMsg("success");
				r.setResult(userById);
			} else {
				System.out.println("登录失败");
			}
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			System.out.println("登陆失败2");
			e.printStackTrace();
		}

		return r;
	}
	//删除用户登录信息
	public AppResult cleanUser(HttpServletRequest request){
		AppResult r = new AppResult();
		request.getSession().removeAttribute("UID");
		request.getSession().removeAttribute("USERTRUENAME");
		return r;
	}

	private static final String VALIDATE_EMAIL = "SELECT COUNT(*) FROM c_user WHERE EMAIL = ?";

	public AppResult validateEmail(HttpServletRequest request) {
		AppResult r = new AppResult();
		String email = request.getParameter("email");
		int queryForInt = jt.queryForInt(VALIDATE_EMAIL, email);
		System.out.println(queryForInt);
		r.setTotleCount(queryForInt);
		return r;
	}

	private static final String VALIDATE_NAME = "SELECT COUNT(*) FROM c_user WHERE USERTURENAME = ?";

	public AppResult validateName(HttpServletRequest request) {
		AppResult r = new AppResult();
		String name = request.getParameter("name");
		int queryForInt = jt.queryForInt(VALIDATE_NAME, name);
		System.out.println(queryForInt);
		r.setTotleCount(queryForInt);
		return r;
	}

	private static final String SELECT_CREATE_UID = "select max(UID) as UID from c_user";
	private static final String CREATE_USER = "insert into c_user(UID,EMAIL,USERTRUENAME,USERINDEXNAME,PASSWD,USERSEX,CITY,DATELINE) VALUES(?,?,?,?,?,?,?,now())";
	private static final String CREATE_USER_TIP = "insert into c_user_tip(UID,TIP)values(?,?)";
	private static final String CREATE_USER_PHOTO = "insert into c_user_hdphoto(UID,DATELINE)values(?,now())";
	private static final String CREATE_USER_STATUS = "insert into c_user_status(UID,STATUS,EMAILSTATE) values (?,0,0)";
	private static final String CREATE_USER_SHETTING = "insert into c_user_setting(UID,SETBASE,SETSTATUS,SETSCHOOL,SETTIP,SETMSG,SETPUBLISH,SETROUND,SETACTIVITY)values(?,1,1,1,1,1,1,1,1)";

	public AppResult createUser(HttpServletRequest request) {
		AppResult r = new AppResult();
		String email = request.getParameter("email");
		String name = request.getParameter("name");
		String passwd = request.getParameter("passwd");
		String sexual = request.getParameter("sexual");
		String city = request.getParameter("city");
		String mytips = request.getParameter("mytips");
		System.out.println(mytips);
		System.out.println(email + " " + name + " " + passwd + " " + mytips);
		long time = new Date().getTime();
		String LASTUID = (String) jt.queryForList(SELECT_CREATE_UID).get(0)
				.get("UID");
		Integer uidInt = Integer.valueOf(LASTUID);
		uidInt = uidInt + 1;
		String UID = String.valueOf(uidInt);
		System.out.println("注册用户UID: " + UID);
		String headerStr = PinyingUtil.hanziToHeaderPinyin(name);
		String allStr = PinyingUtil.hanziToPinyin(name);;
		String indexStr = name+headerStr+allStr;
		
		int update = jt.update(CREATE_USER, UID, email, name,indexStr, MD5
				.getMD5ofStr(passwd), sexual, city);
		String[] tipArray = mytips.split(",");
		for (String tip : tipArray) {
			jt.update(CREATE_USER_TIP, UID, tip);
		}
		jt.update(CREATE_USER_PHOTO, UID);
		jt.update(CREATE_USER_STATUS, UID);
		jt.update(CREATE_USER_SHETTING, UID);
		return r;
	}

	// 获得好友同步率
	private static final String GET_COMMONACT_COUNT = "select COUNT(a.ACTID) from (select ACTID from c_activity_attention where UID =? and STATUS=1) as a cross join (select ACTID from c_activity_attention where UID =? and STATUS=1) as b on a.ACTID=b.ACTID ";
	private static final String GET_COMMONFRIEND_COUNT = "select COUNT(a.FRIENDID) from (select FRIENDID from c_friend where STATE=1 and UID = ?) as a cross join (select FRIENDID from c_friend where STATE=1 and UID = ?)as b on a.FRIENDID = b.FRIENDID;";
	private static final String SELECT_COMMONROUND_COUNT_BY_UID = "select COUNT(a.ROUNDID) from (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as a cross join (select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where r.STATUS=0 and ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?) as b on a.ROUNDID = b.ROUNDID";

	public double getCommonState(String uid, String friendId) {
		// Ma共同好友数量Ma=25%
		int Ma = jt.queryForInt(GET_COMMONFRIEND_COUNT, uid, friendId);
		// Mb共同拥有的公共圈子数量Mb=25%
		int Mb = jt.queryForInt(SELECT_COMMONROUND_COUNT_BY_UID, uid, friendId);
		// Mc共同参加过的活动数量Mc=30%
		int Mc = jt.queryForInt(GET_COMMONACT_COUNT, uid, friendId);
		// Md指定分享数量Md=20%
		int Md = 47;
		// 各项所占比例
		double Ra = 1;
		double Rb = 5;
		double Rc = 6;
		double Rd = 0.5;
		// 各项判定条件
		double Sa = Ma * Ra;
		double Sb = Mb * Rb;
		double Sc = Mc * Rc;
		double Sd = Md * Rd;
		if (Sa >= 25)
			Sa = 25;
		if (Sb >= 25)
			Sb = 25;
		if (Sc >= 30)
			Sc = 30;
		if (Sd >= 20)
			Sd = 20;
		double Eab = Sa + Sb + Sc + Sd;
		return Eab;
	}

	// 小名片
	private static final String SELECT_PERSONIF = "select USERTRUENAME,s.STATUS,MIMG from c_user_hdphoto chd,c_user u,c_user_status s where chd.UID=u.UID and s.UID=u.UID and u.UID=? limit 0,1";
	private static final String SELECT_NEAR_ACTIVITY = "select TITLE,a.ACTID from c_activity a,c_activity_attention caa where caa.ACTID=a.ACTID and caa.status = 1 and caa.UID = ? and (SELECT TIMEDIFF(now(),a.BEGINTIME) < 72) order by a.BEGINTIME DESC limit 0,1";
	private static final String COUNT_ATTENTION = "select count(*) from c_friend where FRIENDID=? and STATE=0";
	private static final String SELECT_ROUND_BY_UID = "select r.ROUNDID,r.ROUNDNAME,rp.RPATH from c_round r,c_round_attention ra,c_round_photo rp where ra.ROUNDID=r.ROUNDID and r.ROUNDID=rp.RID and ra.UID=?";
	private static final String SELECT_COUNT_ROUND_BY_UID_ROUNDID = "select COUNT(*) from c_round r,c_round_attention ra where ra.ROUNDID=r.ROUNDID and ra.UID=? and r.ROUNDID=?";

	public AppResult personIF(HttpServletRequest request) {
		String UID = request.getParameter("UID");
		AppResult r = new AppResult();
		Map<String, Object> queryForMap = new HashMap<String, Object>();
		// 得到用户信息
		List<Map<String, Object>> queryForList = jt.queryForList(
				SELECT_PERSONIF, UID);
		// 得到该用户近期活动
		List<Map<String, Object>> queryForList2 = jt.queryForList(
				SELECT_NEAR_ACTIVITY, UID);
		//隐私
		boolean limit= CompareUtil.compare(jt, (String) request.getSession().getAttribute("UID"), UID, LimitType.COMPARE_MYROUND_LIMITS.toString());
		// 得到该用户圈子列表
		List<Map<String, Object>> roundList = null;
		if(limit){
			System.out.println("啊啊啊啊");
			roundList = jt.queryForList(SELECT_ROUND_BY_UID, UID);
		}else{
			roundList = new ArrayList<Map<String,Object>>();
		}
		
		List<Map<String, Object>> commonList = new ArrayList<Map<String, Object>>();
		double commonStatus;
		// 判断用户是否登录
		if (request.getSession().getAttribute("UID") != null) {
			// 获取同步率
			/*
			 * 注意此处同步率对自己的判断、
			 */
			commonStatus = getCommonState((String) request.getSession()
					.getAttribute("UID"), UID);
			// 获取共同圈子
			Iterator<Map<String, Object>> it = roundList.iterator();
			while (it.hasNext()) {
				Map<String, Object> next = it.next();
				int a = (Integer) next.get("ROUNDID");
				int count = jt.queryForInt(SELECT_COUNT_ROUND_BY_UID_ROUNDID,
						request.getSession().getAttribute("UID"), a);
				if (count == 1) {
					commonList.add(next);
				}
			}
		} else {
			// 未登陆情况对同步率初始化
			commonStatus = 0;
		}
		queryForMap.put("commonStatus", commonStatus);
		int attionedCount = 0;
		if (!queryForList.isEmpty()) {
			queryForMap.putAll(queryForList.get(0));
		}
		if (!queryForList2.isEmpty()) {
			queryForMap.putAll(queryForList2.get(0));
		} else {
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("TITLE", "null");
			m.put("ACTID", 0);
			queryForMap.putAll(m);
		}
		queryForMap.put("roundList", roundList);
		queryForMap.put("roundListSize", roundList.size());
		queryForMap.put("commonList", commonList);
		queryForMap.put("commonListSize", commonList.size());
		attionedCount = jt.queryForInt(COUNT_ATTENTION, UID);
		queryForMap.put("attionedCount", attionedCount);
		System.out.println(queryForMap);
		r.setStatus(200);
		r.setMsg("success");
		r.setResult(queryForMap);
		return r;
	}

	public AppResult logout(HttpServletRequest request) {
		System.out.println("退出");
		request.getSession().invalidate();
		AppResult r = new AppResult();
		r.setMsg("success");
		r.setStatus(200);
		return r;
	}

	@Override
	public Integer add(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer delete(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(HttpServletRequest request, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	private static final String QUERY_USER_BY_UID = "select PROVINCE,CITY,BIRTH,USERID,MIMG,USERTRUENAME,c_user.SCHOOL,c_user.UID,c_user.email,c_user.USERSEX from c_user,c_user_hdphoto where c_user_hdphoto.UID = c_user.UID and c_user.UID = ?";
	private static final String QUERY_USER_TIP_BY_UID = "SELECT t.TIPID,t.TIPNAME  FROM c_user_tip u ,c_tip t where u.TIP = t.TIPID AND u.UID = ?;";
	private static final String QUERY_USER_PHOTO = "SELECT * FROM c_user_hdphoto WHERE UID = ?";
	private static final String QUERY_USER_TIP = "SELECT * from c_tip t,c_user_tip ut where t.TIPID = ut.";
	private static final String QUERY_USER_SETTING = "SELECT * FROM c_user_setting WHERE UID = ?";
	//被关注数量
	private static final String GET_ATTENTIONCOUNT = "select COUNT(UID) as ATTENTIONCOUNT from c_friend where FRIENDID = ? and state =0";
	//关注数量
	private static final String GET_ATTENTCOUNT = "SELECT COUNT(FRIENDID) FROM c_friend WHERE UID = ? AND STATE = 0";
	//好友数量
	private static final String GET_FRIENDCOUNT = "SELECT COUNT(FRIENDID) FROM c_friend WHERE UID = ? AND STATE = 1";
	//查找学校名字
	private static final String GET_SCHOOLNAME = "select SCHOOLNAME from m_school where SCHOOLID=?";
	//查找省市名字
	private static final String GET_CITYNAME="select CITYNAME from m_city where CODE=?";
	private static final String GET_PROVINCENAME="select PROVINCESNAME from m_provinces where CODE=?";
	//获取用户一句话状态
	private static final String GET_USER_SPEAK="select DETAIL from c_user_speak where UID=?";
	
	//获取个人主页的静态信息
	public Map getUserById(String UID,String homePersonId) {
		//若homePersonId不为空则进入的是其他人主页
		boolean compareInfo=true;
		boolean compareSchool=true;
		boolean compareStatus=true;
		boolean compareTips=true;
		double commonStatus;
		String commonStatusStr = null;
		//String userId=UID;
		if(homePersonId!=null){
			
			//判断是用户是否+隐私
			CompareUtil c = new CompareUtil();
			compareInfo = c.compare(jt,UID,homePersonId, LimitType.COMPARE_BASEINFO_LIMITS.toString());
			compareSchool = c.compare(jt,UID,homePersonId, LimitType.COMPARE_SCHOOLINFO_LIMITS.toString());
			compareStatus = c.compare(jt,UID,homePersonId, LimitType.COMPARE_MYSTATUS_LIMITS.toString());
			compareTips = c.compare(jt,UID,homePersonId, LimitType.COMPARE_MYTIPS_LIMITS.toString());
			commonStatus = getCommonState(homePersonId, UID);
			commonStatusStr=commonStatus+"%";
			UID=homePersonId;
		}
		
		
		List userDB = jt.queryForList(QUERY_USER_BY_UID, UID);
		
		System.out.println(userDB);
		Map user = (Map) userDB.get(0);
		CUserExample userExample = new CUserExample();
		userExample.createCriteria().andUidEqualTo(UID);
		List<CUser> mUserDBList = userMapper
				.selectByExampleWithBLOBs(userExample);
		if (mUserDBList.size() == 1) {
			CUser cUser = mUserDBList.get(0);
			user.put("userdb", cUser);
		} else {
		}
		List userTipList = jt.queryForList(QUERY_USER_TIP_BY_UID, UID);
		Map userPhotoMap = jt.queryForList(QUERY_USER_PHOTO, UID).get(0);
		Map userSettingMap = jt.queryForList(QUERY_USER_SETTING, UID).get(0);
		//标签隐私
		if(!compareTips){
			userTipList.clear();
			Map m = new HashMap();
			m.put("TIPNAME", Constants.LIMIT_CANNOT);
			userTipList.add(m);
		}
		user.put("tiplist", userTipList);
		user.putAll(userSettingMap);
		ListIterator it = userTipList.listIterator();
		JSONArray ja = new JSONArray();
		while (it.hasNext()) {
			Map next = (Map) it.next();
			JSONObject a = new JSONObject();
			a.put("tipid", next.get("TIPID"));
			a.put("tipname", next.get("TIPNAME"));
			ja.add(a);
		}
		System.out.println(userTipList + "\n" + ja);
		user.putAll(userPhotoMap);

		user.put("tipJson", ja);
		// 被关注数量
		int attentedCount = jt.queryForInt(GET_ATTENTIONCOUNT, UID);
		// 关注的人的数量
		int attentionCount = jt.queryForInt(GET_ATTENTCOUNT, UID);
		//好友数量
		int friendCount = jt.queryForInt(GET_FRIENDCOUNT,UID);
		user.put("attentedCount", attentedCount);
		user.put("attentionCount", attentionCount);
		user.put("friendCount", friendCount);
		//同步率
		user.put("commonStatusStr", commonStatusStr);
		//生日
		Date date = (Date) user.get("BIRTH");
		String dateStr=TimeUtil.DateFormatForDay(date);
		
		//城市、省份、学校
		String schoolId=(String) user.get("SCHOOL");
		String schoolName=(String)jt.queryForMap(GET_SCHOOLNAME,schoolId).get("SCHOOLNAME");
		String cityId = (String) user.get("CITY");
		String provinceId=(String) user.get("PROVINCE");
		String cityName = (String) jt.queryForMap(GET_CITYNAME,cityId).get("CITYNAME");
		String provinceName = (String) jt.queryForMap(GET_PROVINCENAME,provinceId).get("PROVINCESNAME");
		//一句话状态
		String userStatu = (String) jt.queryForMap(GET_USER_SPEAK,UID).get("DETAIL");
		//基本信息隐私
		if(!compareInfo){
			provinceName=Constants.LIMIT_CANNOT;
			dateStr=Constants.LIMIT_CANNOT;
			cityName="";
		}
		//学校隐私
		if(!compareSchool){
			schoolName=Constants.LIMIT_CANNOT;
		}
		//一句话状态隐私
		if(!compareStatus){
			userStatu=Constants.LIMIT_CANNOT;
		}
		
		user.put("dateStr", dateStr);
		user.put("cityName", cityName);
		user.put("provinceName", provinceName);
		user.put("schoolName", schoolName);
		user.put("userStatu", userStatu);
		user.put("UID", UID);
		//判断男女
		if(Integer.valueOf((String)user.get("USERSEX"))==1){
			user.put("userSex","front/images/person/person_icon.png");
			System.out.println("男");
		}
		else{
			user.put("userSex", "front/images/person/person_icon_l.png");
			System.out.println("女");
		}
		
		//将user传出
		System.out.println(user);
		return user;
	}

	private static final String QUERY_USER_MAP = "select * from c_user where UID = ?";

	public Map getUserMap(String userid) {
		// TODO Auto-generated method stub
		System.out.println(userid + "@@@@@@@@@@@@@@@@@@@@@@@@");
		return jt.queryForList(QUERY_USER_MAP, userid).get(0);
	}

	public String getPhotoByUID(String userid) {
		// TODO Auto-generated method stub
		Map userPhotoMap = jt.queryForList(QUERY_USER_PHOTO, userid).get(0);
		return (String) userPhotoMap.get("SIMG");
	}

	private static final String AJAX_QUERY_PHOTO = "SELECT LIMG FROM c_user_hdphoto where UID = ?";

	public AppResult getPhotoByUIDandTYPE(HttpServletRequest request) {
		AppResult r = new AppResult();
		String type = request.getParameter("type");
		String uid = request.getParameter("uid");
		List photoDB = jt.queryForList(AJAX_QUERY_PHOTO, uid);
		if (!photoDB.isEmpty()) {
			Map pMap = (Map) photoDB.get(0);
			String photoSrc = (String) pMap.get("LIMG");
			r.setStatus(200);
			System.out.println(photoSrc);
			JSONObject jo = new JSONObject();
			jo.put("photo", photoSrc);
			r.setResult(jo);
		}
		return r;
	}

	private static final String SELECT_COMMONSTATE = "select COMMONSTATE from c_friend where UID=? and FRIENDID=?";

	public Map<String, Object> getCommonStatus(String UID, String FID) {
		List<Map<String, Object>> queryForList = jt.queryForList(
				SELECT_COMMONSTATE, UID, FID);
		if (!queryForList.isEmpty()) {
			return queryForList.get(0);
		} else {
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("COMMONSTATE", 0);
			return hashMap;
		}
	}

}
